package main;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JTable;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import sun.beans.editors.BooleanEditor;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.exception.DeleteDBException;
import com.aptech.mwg.etown.pojo.exception.SelectDBException;
import com.aptech.mwg.etown.server.business.AccountManager;
import com.aptech.mwg.etown.server.dao.impl.AccountDAOImpl;

import java.awt.Component;
import javax.swing.Box;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ScrollPaneConstants;
import javax.swing.JInternalFrame;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class accManager extends JPanel {
	private JTable table;
	
	private JTextField txtuser;
	private JTextField txtEmail;
	private JPanel panelsearch ;
	private int idacc;

	public int getIdacc() {
	    return idacc;
	}

	public void setIdacc(int idacc) {
	    this.idacc = idacc;
	}

	public accManager() {
		setBorder(null);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{147, 0, 40, 39, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		Component horizontalGlue = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
		gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
		gbc_horizontalGlue.gridx = 0;
		gbc_horizontalGlue.gridy = 0;
		add(horizontalGlue, gbc_horizontalGlue);
		
		final JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				
			},
			new String[] {
				"check", "AccountID", "UserName", "Email", "Phone", "Address"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				true, false, true, true, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(49);
		 table.getColumnModel().getColumn(1).setMinWidth(0);
		   table.getColumnModel().getColumn(1).setMaxWidth(0);
		   table.getColumnModel().getColumn(1).setWidth(0);

		table.getColumnModel().getColumn(1).setPreferredWidth(15);
		table.getColumnModel().getColumn(2).setPreferredWidth(114);
		table.getColumnModel().getColumn(3).setPreferredWidth(114);
		table.getColumnModel().getColumn(4).setPreferredWidth(114);
		table.getColumnModel().getColumn(5).setPreferredWidth(114);
		JCheckBox check = new JCheckBox();
		table.getColumn("check").setCellEditor(new DefaultCellEditor(check));
		
		
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
			    e.consume();
			    int row = table.getSelectedRow();
		            upacc up = new upacc();
		            idacc = Integer.parseInt(table.getValueAt(row, 1).toString());
		            up.setAccID(idacc);
		            
		            //up.setAccID(Integer.parseInt(table.getValueAt(row, 1).toString()));
		            System.out.println(table.getValueAt(row, 1).toString());
			}
		});
		scrollPane.setViewportView(table);
		scrollPane.setPreferredSize(new Dimension(550, 150));
		panel.add(scrollPane);
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
		scrollPane.setColumnHeaderView(chckbxNewCheckBox);
		
		Component horizontalGlue_1 = Box.createHorizontalGlue();
		GridBagConstraints gbc_horizontalGlue_1 = new GridBagConstraints();
		gbc_horizontalGlue_1.insets = new Insets(0, 0, 5, 0);
		gbc_horizontalGlue_1.gridx = 2;
		gbc_horizontalGlue_1.gridy = 0;
		add(horizontalGlue_1, gbc_horizontalGlue_1);
		
		Component verticalGlue_1 = Box.createVerticalGlue();
		GridBagConstraints gbc_verticalGlue_1 = new GridBagConstraints();
		gbc_verticalGlue_1.insets = new Insets(0, 0, 5, 5);
		gbc_verticalGlue_1.gridx = 1;
		gbc_verticalGlue_1.gridy = 1;
		add(verticalGlue_1, gbc_verticalGlue_1);
		
		final JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 5);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 2;
		add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panelsearch.setVisible(true);
			}
		});
		panel_1.add(btnSearch);
		
		JButton btnInsert = new JButton("Insert ");
		
		btnInsert.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Insertacc in = new Insertacc();
				in.setVisible(true);
				
			}
		});
		panel_1.add(btnInsert);
		
		JButton btnupdate = new JButton("Update");
		btnupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    upacc up = new upacc();
			    int row = table.getSelectedRow();
			    idacc = Integer.parseInt(table.getValueAt(row, 1).toString());
			    setIdacc(idacc);
			    up.setAccID(idacc);
			    up.setVisible(true);
			    System.out.println();
			}
		});
		
		panel_1.add(btnupdate);
		
		JButton btndelete = new JButton("Delete");
		btndelete.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent e) {
			    boolean result = false;
			    int row = table.getSelectedRow();
			    idacc = Integer.parseInt(table.getValueAt(row, 1).toString());
			    setIdacc(idacc);
			    Account account  = new Account();
			    account.setAccountId(getIdacc());
			    AccountDAOImpl daoimpl = new AccountDAOImpl();
			    try {
				result = daoimpl.delete(account);
				if(result == true){
				    System.out.println("succes");
				}
				else{
				    System.out.println("fail");
				}
			    } catch (DeleteDBException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			    }
			    table.removeAll();
			    loadtable();
		    }
		
	
		});
		
		panel_1.add(btndelete);
		
		JButton btncancel = new JButton("Cancel");
		
		btncancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panel.setVisible(false);
				panel_1.setVisible(false);
		
			}
		});
		panel_1.add(btncancel);
		
		panelsearch = new JPanel();
		GridBagConstraints gbc_panelsearch = new GridBagConstraints();
		gbc_panelsearch.insets = new Insets(0, 0, 0, 5);
		gbc_panelsearch.fill = GridBagConstraints.BOTH;
		gbc_panelsearch.gridx = 1;
		gbc_panelsearch.gridy = 3;
		add(panelsearch, gbc_panelsearch);
		GridBagLayout gbl_panelsearch = new GridBagLayout();
		gbl_panelsearch.columnWidths = new int[]{0, 39, 0, 0};
		gbl_panelsearch.rowHeights = new int[]{0, 0, 0, 0};
		gbl_panelsearch.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_panelsearch.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		panelsearch.setLayout(gbl_panelsearch);
		
		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(0, 0, 5, 5);
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 0;
		panelsearch.add(panel_3, gbc_panel_3);
		panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblUsername = new JLabel("UserName: ");
		panel_3.add(lblUsername);
		
		txtuser = new JTextField();
		panel_3.add(txtuser);
		txtuser.setColumns(10);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_1);
		
		JLabel lblCompany = new JLabel("Email");
		panel_3.add(lblCompany);
		
		txtEmail = new JTextField();
		panel_3.add(txtEmail);
		txtEmail.setColumns(10);
		
		Component verticalGlue_3 = Box.createVerticalGlue();
		GridBagConstraints gbc_verticalGlue_3 = new GridBagConstraints();
		gbc_verticalGlue_3.insets = new Insets(0, 0, 5, 5);
		gbc_verticalGlue_3.gridx = 1;
		gbc_verticalGlue_3.gridy = 1;
		panelsearch.add(verticalGlue_3, gbc_verticalGlue_3);
		
		JPanel panel_4 = new JPanel();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.insets = new Insets(0, 0, 0, 5);
		gbc_panel_4.fill = GridBagConstraints.BOTH;
		gbc_panel_4.gridx = 1;
		gbc_panel_4.gridy = 2;
		panelsearch.add(panel_4, gbc_panel_4);
		
		panelsearch.setVisible(false);
		JButton btnOk = new JButton("Ok");
		btnOk.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			AccountManager accmanager = new AccountManager();
			DefaultTableModel model = (DefaultTableModel)table.getModel();
			while(model.getRowCount() > 0)
			{
			    model.removeRow(0);
			}

			List<Account> list = accmanager.Search_condition(txtuser.getText(),txtEmail.getText());
			for(Account acc: list){
			int AccountID = acc.getAccountId();
			String UserName = acc.getLoginName();
			String Email = acc.getEmail();
			String Phone = acc.getPhone();
			String Address =acc.getAddress();
			Object[] newRow = new Object[] {new Boolean(false),AccountID, UserName, Email, Phone, Address};
			model.addRow(newRow);
			System.out.println("load success" );
			}
		    }
		});
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
			}
		});
		panel_4.add(btnOk);
		
		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		panel_4.add(horizontalStrut_2);
		
		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    txtEmail.getText();
			    txtuser.getText();
			}
		});
		btnReset.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		panel_4.add(btnReset);
		
		Component horizontalStrut_4 = Box.createHorizontalStrut(20);
		panel_4.add(horizontalStrut_4);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				panelsearch.setVisible(false);
				loadtable();
			}
		});
		panel_4.add(btnCancel);
		
		
	}
	
	public void loadtable(){
	    table.removeAll();
	    	AccountManager accmanager = new AccountManager();
		DefaultTableModel model = (DefaultTableModel)table.getModel();
		model.setRowCount(0);
		List<Account> listAcc = accmanager.list();
		for (Account account : listAcc) {
		    	int AccountID = account.getAccountId();
			String UserName = account.getLoginName();
			String Email = account.getEmail();
			String Phone = account.getPhone();
			String Address =account.getAddress();
			Object[] newRow = new Object[] {new Boolean(false), AccountID,UserName, Email, Phone, Address};
			model.addRow(newRow);
			System.out.println("load success" );
		}
		table.repaint();
	 }
	
}
